package com.chongchong.common.tool;

import com.chongchong.common.config.IErrorCode;
import com.chongchong.common.enums.ApiErrorCodeEx;

/**
 * @author 作者: Elvira
 * @date 创建时间: 2020年12月8日 下午7:46:18
 *
 *       自定义错误类
 */
public class ApiException extends RuntimeException {

	private static final long serialVersionUID = 1;

	private IErrorCode errorCode;

	public ApiException(IErrorCode errorCode) {
		super(errorCode.getErrMessage());
		this.errorCode = errorCode;
	}

	public ApiException(IErrorCode errorCode, int httpStatus) {
		super(errorCode.getErrMessage());
		this.errorCode = errorCode;
	}

	public ApiException(String errorCode, String message) {
		super(message);

		this.errorCode = new ApiErrorCodeEx(errorCode, message);
	}

	public ApiException(IErrorCode errorCode, Throwable cause) {
		super(errorCode.getErrMessage(), cause);
		this.errorCode = errorCode;
	}

	public ApiException(String errorCode, Throwable cause) {
		super(cause);
		this.errorCode = new ApiErrorCodeEx(errorCode, cause.getMessage());
	}

	// TODO 需要删除此方法
	public ApiException(String message) {
		super(message);
		this.errorCode = new ApiErrorCodeEx("UNKNOWN", message);
	}

	public IErrorCode getErrorCode() {
		return errorCode;
	}
}
